package io.renren.modules.task.controller;

import com.ioserver.bean.Struct_TagInfo;
import com.sun.jna.WString;
import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.modules.task.wcs.IOServerBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ioserver.IOServerApi;
import ioserver.IOServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Vector;

/**
 * @Author: 蔡翔
 * @Date: 2019/8/27 8:46
 * @Version 1.0
 */
@Api(description = "ioserverTest")
@RestController
@RequestMapping("/io")
public class IoserverTestController {

    IOServerService ioServerService = new IOServerService();

    @Autowired
    IOServerBean ioServerBean;

    @ApiOperation(value = "ioserver-getConnection", notes = "ioserver-getConnection")
    @GetMapping("/getConnection")
    public R getConnection() throws Exception{
        Integer result = ioServerService.getConnection();
        if(result == 0){
            return R.ok().put("result",0).put("msg","连接ioserver成功");
        }else if(result == -2){
            return R.error().put("result",-2).put("msg","ioserver获取句柄失败，请查看IOServer 是否已经开启");
        }else {
            return R.error().put("result",-1).put("msg","ioserver异常");
        }
    }

    @ApiOperation(value = "ioserver-getValueByName", notes = "ioserver-getValueByName")
    @GetMapping("/getValueByName")
    public com.ioserver.bean.Union_DataType.ByValue getValueByName() throws Exception{
        return ioServerService.getValueByName("Tag1");
    }

    @ApiOperation(value = "ioserver-getBatchValueByNames", notes = "ioserver-getBatchValueByNames")
    @GetMapping("/getBatchValueByNames")
    public List<com.ioserver.bean.Union_DataType.ByValue> getBatch() throws Exception{
        return ioServerService.getBatchValueByNames("Tag1,Tag2,Tag3");
    }

    @ApiOperation(value = "ioserver-subscribeAllTags", notes = "ioserver-subscribeAllTags")
    @GetMapping("/subscribeAllTags")
    public Vector<WString> subscribeAllTags() throws Exception{
        return ioServerService.getAllTagName();
    }

    @ApiOperation(value = "ioserver-disConnect", notes = "ioserver-disConnect")
    @GetMapping("/disConnect")
    public R disConnect() throws Exception{
        Integer result = ioServerService.funcDisConnect();
        if(result == -1){
            return R.error().put("result",-1).put("msg","ioserver 已经断开连接");
        }
        return R.ok().put("result",0).put("msg","ioserver断开连接成功");
    }



    @ApiOperation(value = "ioserver-setTagValue", notes = "ioserver-setTagValue")
    @RequestMapping("/setTagValue/{tagValue}")
    public com.ioserver.bean.Union_DataType.ByValue setTagValue(@PathVariable("tagValue") String tagValue) throws Exception{
        Integer result = ioServerService.setTagValue("Tag6",tagValue,4);
        if(result == -1){
            throw new RRException("可能：1.IOServer未连接；2.tagName未空；3.tagValue为空");
        }else if(result == -2){
            throw new RRException("IOServer 写入类型 暂不支持");
        }else {
            Thread.sleep(1000);
            return ioServerService.getValueByName("Tag6");
        }
    }

    @ApiOperation(value = "ioserver-subscribeTagValueByTagName", notes = "ioserver-subscribeTagValueByTagName")
    @GetMapping("/subscribeTagValueByTagName")
    public R subscribeTagValueByTagName() throws Exception{
        Struct_TagInfo result = ioServerService.getSubscribeTagValueByTagName("Tag2");
        return R.ok().put("result",result.TagValue.TagValue).put("TagId",result.TagID);
    }

    @ApiOperation(value = "ioserver-getSubscribeAllTagValue", notes = "ioserver-getSubscribeAllTagValue")
    @GetMapping("/getSubscribeAllTagValue")
    public R getSubscribeAllTagValue() throws Exception{
        Struct_TagInfo[] result = ioServerService.getSubscribeAllTagValue();
        return R.ok().put("result",result);
    }

    @ApiOperation(value = "ioserver-listenTagValueByTagName", notes = "ioserver-listenTagValueByTagName")
    @GetMapping("/listenTagValueByTagName")
    public R listenTagValueByTagName() throws Exception{
        String tagName = "Tag1";
        boolean flag = ioServerService.listenTagValueByTagName(tagName);
        if(flag){
            return R.ok().put("result","监听"+tagName+" 变量,成功");
        }else {
            return R.ok().put("result","监听"+tagName+" 变量,失败");
        }
    }

    @ApiOperation(value = "ioserver-IoserverBean", notes = "ioserver-IoserverBean")
    @GetMapping("/ioserverBean")
    public R ioserverBean() throws Exception{
        return R.ok().put("result",ioServerBean.getAllTagName());
    }
}
